package com.partners.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.partners.constant.ErrorMessageEnum;
import com.partners.dao.basic.PointsLogDao;
import com.partners.dao.basic.UserDao;
import com.partners.dao.bo.PointsLogDO;
import com.partners.dao.bo.UserDO;
import com.partners.dao.dto.PointInputDTO;
import com.partners.dao.dto.PointsOutputDTO;
import com.partners.model.BusinessException;
import com.partners.model.Paging;
import com.partners.service.PointsService;
import com.partners.utils.JsonUtils;
import com.partners.utils.TimestampUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author liangyongle
 * @date 2018-12-20
 */
@Service
@Slf4j
public class PointsServiceImpl implements PointsService {


    @Resource
    private PointsLogDao pointsLogDao;

    @Resource
    private UserDao userDao;

    /**
     * 获取积分列表
     * @param     pointInputDTO PointInputDTO
     *            type Integer  //类型  0:个人查询，1:所有查询，2:推荐查询
     *            title String
     *            startTime String
     *            endTime String
     * @return Object
     */

    @Override
    public Map<String, Object> getPointsList(Paging paging, PointInputDTO pointInputDTO) {
        PageHelper.startPage(paging.getPage(),paging.getRows());
        if(pointInputDTO.getTitle() != null){
            pointInputDTO.setTitle("'%"+pointInputDTO.getTitle()+"%'");
        }

        Map<String,Object> allList = new HashMap<>();
        List<Map<String,Object>> list;
        //判断是否为管理员
        if(pointInputDTO.getAll() == 1){
            //管理员
            list = pointsLogDao.getPointsRecommendList(pointInputDTO.getTitle(),pointInputDTO.getType(), pointInputDTO.getStartTime(),pointInputDTO.getEndTime());
        }else{
            //本人
            list = pointsLogDao.getPointsList(pointInputDTO.getUid(),pointInputDTO.getTitle(),pointInputDTO.getStartTime(),pointInputDTO.getEndTime());
        }
        Page page = (Page) list;
        Object total = page.getTotal();
        Map<Object, List<Map<String, Object>>> timeList = JsonUtils.toMonthList(list);
        allList.put("total",total);
        allList.put("list",timeList);
        return allList;
    }



//    @Override
//    public Map<String, Object> getPointsList(Paging paging,Integer type,  Integer uid, String title, String startTime, String endTime) {
//        PageHelper.startPage(paging.getPage(),paging.getRows());
//        if(title != null){
//            title = "'%"+title+"%'";
//        }
//        Map<String,Object> allList = new HashMap<>();
//        List<Map<String,Object>> list;
//        switch(type){
//            case 0 : list = pointsLogDao.getPointsList(uid,title,startTime,endTime); break; //个人
//            case 1 : list = pointsLogDao.getAllPointsList(title,startTime,endTime); break; //所有
//            case 2 : list = pointsLogDao.getPointsRecommendList(title,startTime,endTime); break; //推荐
//            default: list = pointsLogDao.getPointsList(uid,title,startTime,endTime); break; //个人
//        }
//        Page page = (Page) list;
//        Object total = page.getTotal();
//        Map<Object, List<Map<String, Object>>> timeList = JsonUtils.toMonthList(list);
//        allList.put("total",total);
//        allList.put("list",timeList);
//        return allList;
//    }


    /**
     * 获取积分详情
     * @param id Integer
     * @return Object
     */
    @Override
    public PointsOutputDTO getPointsDetail(Integer id) {
        //1.查询订单详情
        //2.判断上订单类型
        //1).普通订单  2).成交订单 3).推荐分成订单
        PointsLogDO pointsLog = pointsLogDao.selectByPrimaryKey(id);
        if(pointsLog == null){
            throw BusinessException.of(ErrorMessageEnum.RECORD_NOT_FOUND);
        }

        String timeFormat = "yyyy-MM-dd HH:mm:ss";
        String addtime = TimestampUtils.getDateTimeOfTimestamp(pointsLog.getAddtime(),timeFormat);
        //装载输出
        PointsOutputDTO detail = new PointsOutputDTO();
        detail.setId(pointsLog.getId());
        detail.setUid(pointsLog.getUid());
        detail.setType(pointsLog.getType().intValue());
        detail.setSource(pointsLog.getSource().intValue());
        detail.setTitle(pointsLog.getTitle());
        detail.setPoint(pointsLog.getPoint());
        detail.setAddtime(addtime);
        //查询我本人
        UserDO user = userDao.selectByPrimaryKey(pointsLog.getUid());
        detail.setUserName(user.getName());

        if(pointsLog.getSource() == 0){
            //成交客户
            Map<String,Object> order =  pointsLogDao.selectOrderByOrderId(pointsLog.getOrderid());
            if(order != null){
                detail.setOrderNo((Long) order.get("order_no"));
                detail.setAddName((String) order.get("addName"));
            }
        }else if(pointsLog.getSource() == 1){
            //发展合伙人
            UserDO parner = userDao.selectByPrimaryKey(pointsLog.getOrderid());
            if(parner != null){
                detail.setParnerName(parner.getName());
            }
        }else if(pointsLog.getSource() == 2){
            //推荐成交
            Map<String,Object> order =  pointsLogDao.selectOrderByOrderId(pointsLog.getOrderid());
            if(order != null){
                detail.setOrderNo((Long) order.get("order_no"));
                detail.setAddName((String) order.get("addName"));
            }
            UserDO parner = userDao.selectByPrimaryKey(pointsLog.getOrderid());
            if(parner != null){
                detail.setParnerName(parner.getName());
            }
        }
        log.info("detail{}",detail);


        return detail;
//        Map<String,Object> detail = pointsLogDao.getPointDetailById(id);
//        if(detail == null){
//            throw BusinessException.of(ErrorMessageEnum.RECORD_NOT_FOUND);
//        }
//        Integer source = (Integer) detail.get("source");
//        String userName = (String) detail.get("user_name");
//        Integer point = (Integer) detail.get("point");
//        if(source ==2){
//          Map<String,Object> recomon = pointsLogDao.getRecommendDetail((Integer) detail.get("orderid"));
//          if(recomon != null){
//            detail.put("user_name",(String) recomon.get("name"));
//            detail.put("point",(Integer) recomon.get("point"));
//            detail.put("recom_name",userName);
//            detail.put("recom_point",point);
//          }
//        }
//        return detail;
    }


}
